import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/views/layout/Layout";

export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },
  {
    path: "/auth-redirect",
    component: () => import("@/views/login/authredirect"),
    hidden: true,
  },
  {
    path: "/404",
    component: () => import("@/views/errorPage/404"),
    hidden: true,
  },
  {
    path: "/401",
    component: () => import("@/views/errorPage/401"),
    hidden: true,
  },
  {
    path: "",
    component: Layout,
    redirect: "dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index"),
        name: "Dashboard",
        meta: { title: "app.menu.dashboard", icon: "dashboard", affix: true },
      },
    ],
  },
];

export const asyncRoutes = [
  {
    path: "/user",
    component: Layout,
    redirect: "noredirect",
    alwaysShow: true,
    name: "userManage",
    meta: {
      title: "app.menu.user",
      icon: "peoples",
    },
    children: [
      {
        path: "user",
        component: () => import("@/views/user/user"),
        name: "user",
        meta: {
          perms: ["GET /admin/user/list"],
          title: "app.menu.user_user",
          noCache: true,
        },
      },
      {
        path: "address",
        component: () => import("@/views/user/address"),
        name: "address",
        meta: {
          perms: ["GET /admin/address/list"],
          title: "app.menu.user_address",
          noCache: true,
        },
      },
    ],
  },

  {
    path: "/goods",
    component: Layout,
    redirect: "noredirect",
    alwaysShow: true,
    name: "goodsManage",
    meta: {
      title: "app.menu.goods",
      icon: "message",
    },
    children: [
      {
        path: "order",
        component: () => import("@/views/goods/order"),
        name: "order",
        meta: {
          perms: [
            "GET /admin/order/list",
            "GET /admin/order/detail",
            "POST /admin/order/ship",
            "POST /admin/order/refund",
            "POST /admin/order/delete",
            "POST /admin/order/reply",
          ],
          title: "app.menu.mall_order",
          noCache: true,
        },
      },
      {
        path: "category",
        component: () => import("@/views/goods/category"),
        name: "category",
        meta: {
          perms: [
            "GET /admin/category/list",
            "POST /admin/category/create",
            "GET /admin/category/read",
            "POST /admin/category/update",
            "POST /admin/category/delete",
          ],
          title: "app.menu.mall_category",
          noCache: true,
        },
      },
      {
        path: "list",
        component: () => import("@/views/goods/list"),
        name: "goodsList",
        meta: {
          perms: ["GET /admin/goods/list", "POST /admin/goods/delete"],
          title: "app.menu.goods_list",
          noCache: true,
        },
      },
      {
        path: "create",
        component: () => import("@/views/goods/create"),
        name: "goodsCreate",
        meta: {
          perms: ["POST /admin/goods/create"],
          title: "app.menu.goods_create",
          noCache: true,
        },
      },
      {
        path: "edit",
        component: () => import("@/views/goods/create"),
        name: "goodsEdit",
        meta: {
          title: "商品编辑",
          noCache: true,
        },
        hidden: true,
      },
    ],
  },

  {
    path: "/config",
    component: Layout,
    redirect: "noredirect",
    alwaysShow: true,
    name: "configManage",
    meta: {
      title: "app.menu.config",
      icon: "example",
    },
    children: [
      {
        path: "issue",
        component: () => import("@/views/sys/issue"),
        name: "issue",
        meta: {
          perms: [
            "GET /admin/issue/list",
            "POST /admin/issue/create",
            "GET /admin/issue/read",
            "POST /admin/issue/update",
            "POST /admin/issue/delete",
          ],
          title: "app.menu.mall_issue",
          noCache: true,
        },
      },
      {
        path: "deliveryNotice",
        component: () => import("@/views/sys/deliveryNotice"),
        name: "deliveryNotice",
        meta: {
          title: "配送须知",
          noCache: true,
          perms: [
            "POST /admin/delivery/notice/list",
            "POST /admin/delivery/notice/create",
            "POST /admin/delivery/notice/update",
            "POST /admin/delivery/notice/delete",
          ],
        },
      },

      {
        path: "mall",
        component: () => import("@/views/config/mall"),
        name: "configMall",
        meta: {
          perms: ["GET /admin/config/mall", "POST /admin/config/mall"],
          title: "店铺配置",
          noCache: true,
        },
      },
      {
        path: "express",
        component: () => import("@/views/config/express"),
        name: "configExpress",
        meta: {
          perms: ["GET /admin/config/express", "POST /admin/config/express"],
          title: "app.menu.config_express",
          noCache: true,
        },
      },
      {
        path: "order",
        component: () => import("@/views/config/order"),
        name: "configOrder",
        meta: {
          perms: ["GET /admin/config/order", "POST /admin/config/order"],
          title: "app.menu.config_order",
          noCache: true,
        },
      },
    ],
  },

  {
    path: "/sys",
    component: Layout,
    redirect: "noredirect",
    alwaysShow: true,
    name: "sysManage",
    meta: {
      title: "app.menu.sys",
      icon: "component",
    },
    children: [
      {
        path: "admin",
        component: () => import("@/views/sys/admin"),
        name: "admin",
        meta: {
          title: "app.menu.sys_admin",
          noCache: true,
        },
      },
      {
        path: 'coupon',
        component: () => import('@/views/promotion/coupon'),
        name: 'coupon',
        meta: {
          // perms: ['GET /admin/coupon/list', 'POST /admin/coupon/create', 'POST /admin/coupon/update', 'POST /admin/coupon/delete'],
          title: 'app.menu.promotion_coupon',
          noCache: true
        }
      },
           {
        path: 'couponDetail',
        component: () => import('@/views/promotion/couponDetail'),
        name: 'couponDetail',
        meta: {
          // perms: ['GET /admin/coupon/list', 'GET /admin/coupon/listuser'],
          title: 'app.menu.promotion_coupon_detail',
          noCache: true
        },
        hidden: true
      },
      {
        path: "log",
        component: () => import("@/views/sys/log"),
        name: "log",
        meta: {
          title: "app.menu.sys_log",
          noCache: true,
        },
      },
      {
        path: "role",
        component: () => import("@/views/sys/role"),
        name: "role",
        meta: {
          title: "app.menu.sys_role",
          noCache: true,
        },
      },
      {
        path: "os",
        component: () => import("@/views/sys/os"),
        name: "os",
        meta: {
          title: "app.menu.sys_os",
          noCache: true,
        },
      },
    ],
  },

  {
    path: "/profile",
    component: Layout,
    redirect: "noredirect",
    alwaysShow: true,
    children: [
      {
        path: "password",
        component: () => import("@/views/profile/password"),
        name: "password",
        meta: { title: "app.menu.profile_password", noCache: true },
      },
      {
        path: "notice",
        component: () => import("@/views/profile/notice"),
        name: "notice",
        meta: { title: "app.menu.profile_notice", noCache: true },
      },
    ],
    hidden: true,
  },

  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
